package com.zzu.presenter.impl;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.zzu.model.model.UserModel;
import com.zzu.model.bean.User;
import com.zzu.model.impl.UserModelImpl;
import com.zzu.model.service.PackageService;
import com.zzu.model.service.UserService;
import com.zzu.presenter.UserResponseListener;
import com.zzu.presenter.presenter.UserPresenter;
import com.zzu.utils.ServiceCreator;
import com.zzu.utils.ToastAndDialogUtils;
import com.zzu.view.view.UserResponseView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-07 20:58
 */
public class UserPresenterImpl implements UserPresenter {

    private static final String TAG = UserPresenterImpl.class.getName();

    UserModel mUserModel = new UserModelImpl();

    UserResponseView mUserResponseView = null;
    Context context;
    private UserService mUserService = ServiceCreator.getRetrofit(UserService.class);

    public UserPresenterImpl(Context context,UserResponseView userResponseView){
        mUserResponseView = userResponseView;
        this.context = context;
    }

    @Override
    public void login(String tel, String password) {
        //加载进度条
        ProgressDialog dialog = ToastAndDialogUtils.showDialog(context,"正在加载中");
        mUserModel.login(tel, password, new UserResponseListener<Boolean>() {
            @Override
            public void onResponse(Boolean response) {
                if(mUserResponseView != null){
                    ToastAndDialogUtils.cancelDialog(dialog);
                    mUserResponseView.onResponse(response);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                if(dialog != null){
                    dialog.setMessage("请求服务器失败"+t);
                }
            }
        });

    }

    @Override
    public void register(User user) {
        //加载进度条
        ProgressDialog dialog = ToastAndDialogUtils.showDialog(context,"正在加载中");
        mUserModel.register(user, new UserResponseListener() {
            @Override
            public void onResponse(Object response) {
                if(mUserResponseView != null){
                    ToastAndDialogUtils.cancelDialog(dialog);
                    mUserResponseView.onResponse(response);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                if(dialog != null){
                    dialog.setMessage("请求服务器失败"+t);
                }
            }
        });
    }

    @Override
    public void checkIfUserExistByTel(String tel) {
        mUserModel.checkIfUserExistByTel(tel, new UserResponseListener<Boolean>() {
            @Override
            public void onResponse(Boolean response) {
                mUserResponseView.onResponse(response);
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });
    }


    @Override
    public void getUserByTel(String tel) {

        Call<User> call = mUserService.getUserByTel(tel);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {

                User user = response.body();
                mUserResponseView.onResponse(user);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.i(TAG,"请求服务器失败\n"+t);
            }
        });

    }
}
